﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core;
using MvcEngine.Mvc.Models.ViewModels;

using MvcEngine.Core.Repositories;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core.Common;
using MvcEngine.Core.Content;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;
using MvcEngine.Core.Base;

namespace MvcEngine.Mvc.BackOffice.Controllers
{
    public class LogController : BaseController
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel List(int? pageNumber)
        {
            if (!pageNumber.HasValue)
                pageNumber = 1;
            if (pageNumber.Value <= 0)
                return new MvcEngineModel { ViewName = "InvalidPageRequest" };

            LoadOptions options = IoC.Resolve<LoadOptions>();
            options.PageNumber = pageNumber.Value;
            options.PageSize = 100;
            IPagination<Log> pagination = CommonManager.Current.GetLogs(options);

            if (pagination.TotalItems == 0)
                return new MvcEngineModel { ViewName = "Empty" };
            if (pageNumber.Value > pagination.TotalPages)
                return new MvcEngineModel { ViewName = "InvalidPageRequest" };
            return new MvcEngineModelItem<IPagination<Log>> { Item = pagination };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(Guid id)
        {
            CommonManager.Current.DeleteLog(id);
            return RedirectToAction("List");
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Clear()
        {
            CommonManager.Current.DeleteLogs();
            return RedirectToAction("List");
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel Details(Guid id)
        {
            var log = CommonManager.Current.GetLog(id);
            if (log == null)
                return new MvcEngineModel();
            else
                return new MvcEngineModelItem<Log>() { Item = log };
        }
    }
}
